Test Failed
Push — master ( bbdab8...28d97f )
by Florian
04:57 queued 02:15
created

getters.js ➔ getConfig   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A getters.js ➔ ... ➔ 0 12 3
1
export const getUser = function(state) {
2
	return state.user;
3
};
4
5
export const isLoggedIn = function(state) {
6
	return state.user !== undefined && state.user !== null;
7
};
8
9
export const getConfig = function(state) {
10
	return function(key = null) {
11
		if (key === null) {
12
			return state.config;
13
		}
14
15
		if (typeof key !== 'string') {
16
			console.log('Comments config key must be a string.');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
17
			return;
18
		}
19
20
		return state.config[key];
21
	}
22
};
23
24
export const isMyComment = function (state) {
25
	return function(comment) {
26
		if (typeof state.user === 'object' && state.user !== null) {
27
			return state.user.id == comment.user_id;
28
		}
29
30
		return false;
31
	}
32
};
33
34
export const getOrderBy = function(state) {
35
	return state.orderBy;
36
};
37
38
export const getComments = function(state) {
39
	return state.comments;
40
};
41
42
export const hasChildren = function(state) {
43
	return function(model, modelId, parentId = null) {
44
		let result = state.comments.filter(function(comment) {
45
			// Has to be weak typed, fucking dynamic types
46
			return comment.model == model
47
				&& comment.foreign_key == modelId
48
				&& comment.parent_id == parentId;
49
		});
50
51
		return result.length > 0;
52
	}
53
};
54
55
export const hasMore = function(state) {
56
	return function(model, modelId, parentId = null) {
57
		let key = model + modelId + parentId;
58
		let pagination = Object.assign({}, state.pagination);
59
60
		if (pagination[key] !== undefined) {
61
			return pagination[key].nextPage;
62
		}
63
64
		return false;
65
	}
66
};
67
68
export const getCommentsForModel = function(state) {
69
	return function(model, modelId, parentId = null) {
70
		let comments = state.comments.filter(function(comment) {
71
			// Has to be weak typed, fucking dynamic types
72
			return comment.model == model
73
				&& comment.foreign_key == modelId
74
				&& comment.parent_id == parentId;
75
		});
76
77
		comments.sort((a, b) => {
78
			switch (state.orderBy) {
79
				case 'newest':
80
					return a.created < b.created;
81
				case 'oldest':
82
					return a.created > b.created;
83
				default:
84
					return a.created < b.created;
85
			}
86
		});
87
88
		return comments;
89
	}
90
};
91
92
export const getPagination = function(state) {
93
	return function (model, modelId, parentId = null) {
94
		let key = model + modelId + parentId;
95
96
		if (state.pagination[key] !== undefined) {
97
			return state.pagination[key];
98
		}
99
100
		return {
101
			page: 1,
102
			nextPage: false,
103
			prevPage: false,
104
			pageCount: 1
105
		}
106
	}
107
}
108